FileUploader = require "./../../../common/file-uploader/index.coffee"

$ () ->
	imgKeyValue1 = null

	new FileUploader {
        click_button: "upload1_btn", # 点击后触发打开上传文件窗口的按钮，与类型为file的按钮绑定
        show_img: "cn_img1",                   # 上传成功后回显的图片ID
        domain: "http://p99igky61.bkt.clouddn.com/",    # bucket 域名，下载资源时用到，**必需**
        browse_button: 'upload1',       # 上传选择的点选按钮ID，**必需**
        container: 'img-wrapper1',       # 上传区域DOM ID，默认是browser_button的父元素，**必需**
    }, {
        FileUploaded: (up, file, info)->
            imgKeyValue1 = info.key
	}
	imgKeyValue2 = null

	new FileUploader {
        click_button: "upload2_btn", # 点击后触发打开上传文件窗口的按钮，与类型为file的按钮绑定
        show_img: "cn_img2",                   # 上传成功后回显的图片ID
        domain: "http://p99igky61.bkt.clouddn.com/",    # bucket 域名，下载资源时用到，**必需**
        browse_button: 'upload2',       # 上传选择的点选按钮ID，**必需**
        container: 'img-wrapper2',       # 上传区域DOM ID，默认是browser_button的父元素，**必需**
    }, {
        FileUploaded: (up, file, info)->
            imgKeyValue2 = info.key
	}
	imgKeyValue3 = null

	new FileUploader {
        click_button: "upload3_btn", # 点击后触发打开上传文件窗口的按钮，与类型为file的按钮绑定
        show_img: "cn_img3",                   # 上传成功后回显的图片ID
        domain: "http://p99igky61.bkt.clouddn.com/",    # bucket 域名，下载资源时用到，**必需**
        browse_button: 'upload3',       # 上传选择的点选按钮ID，**必需**
        container: 'img-wrapper3',       # 上传区域DOM ID，默认是browser_button的父元素，**必需**
    }, {
        FileUploaded: (up, file, info)->
            imgKeyValue3 = info.key
	}
	imgKeyValue4 = null

	new FileUploader {
        click_button: "upload4_btn", # 点击后触发打开上传文件窗口的按钮，与类型为file的按钮绑定
        show_img: "cn_img4",                   # 上传成功后回显的图片ID
        domain: "http://p99igky61.bkt.clouddn.com/",    # bucket 域名，下载资源时用到，**必需**
        browse_button: 'upload4',       # 上传选择的点选按钮ID，**必需**
        container: 'img-wrapper4',       # 上传区域DOM ID，默认是browser_button的父元素，**必需**
    }, {
        FileUploaded: (up, file, info)->
            imgKeyValue4 = info.key
	}

	ClearNullArr = (arr) ->
		_tmpArr = new Array()
		k=0
		for i in arr
			if i != null && i != ''
				_tmpArr[k] = i
				k++

		return _tmpArr



	$("#create_btn").click (e) ->
		picture_arr = new Array(imgKeyValue1,imgKeyValue2,imgKeyValue3,imgKeyValue4)

		picture_arr = ClearNullArr(picture_arr)

		param=
			c_name: $("#c_name").val()
			c_number: $("#c_number").val()
			c_integration: $("#c_integration").val()
			c_picture: picture_arr
			ct_type_id: $("#ct_type_id").val()

		# console.log param

		$.post "/api/admin/commodity/add-commodity",param,(res) ->
			if res.status.errCode is 0
				alert "添加成功"
				window.location.href = '/admin/commodity/index'
			else
				alert res.status.message

	$("#cancel_btn").click (e) ->
		window.location.href = "/admin/commodity/index"